/*index.js*/
var app = getApp();
var a = 1
Page({
  data: {
    indicatorDots: true,
    indicatorColor: "#eee",
    indicatorActiveColor: "rgb(26,125,208)",
    autoplay: true,
    interval: 3000,
    duration: 500,
    low_img: "/statics/img/must_low_price.png",
    bow_price: "/statics/img/bow_price.png",
    search_val: "搜索商品",
    activeIndex: 0,
    used_title: [],
    animationData: {},
    cart_num: app.globalData.cart_num,
    tabBar: app.globalData.tabBar,
    used_shop_list: [],
    list_name: "推荐清单",
    banner_list:[],
    quick_list:[],
    brand_list:[],
    styleText: '',
    seat:'',
    is_index:true,
    balls:[
      { isShow: false, animationData:null },
      { isShow: false, animationData:null }
    ],
    nav_scroll:0,
    order_num:0
  },
  active: function (e) {
    var that = this
    var scrollTop = that.scrollTop||0
    var width = e.currentTarget.offsetLeft / e.currentTarget.id
    this.setData({
      activeIndex: e.currentTarget.id,
      nav_scroll: e.currentTarget.offsetLeft - width * 2,
    })
    wx.createSelectorQuery().select('#list_' + e.currentTarget.id).boundingClientRect(function(rect){
      wx.pageScrollTo({
        scrollTop: rect.top + scrollTop - that.header_height - that.nav_height,//滚动距离计算
      })
    }).exec()
  },
  onPageScroll:function(e){
    var that = this
    var used_title = this.data.used_title
    this.scrollTop = e.scrollTop
    if (e.scrollTop > this.nav_top - this.header_height){
      this.setData({
        styleText: 'position:fixed;top:' + this.header_height + 'px;width:100%;z-index:1001;',
        seat: 'margin-bottom:200rpx;'
      })
    }else{
      this.setData({
        styleText: '',
        seat:''
      })
    }
    for (let i in used_title) {
      wx.createSelectorQuery().select('#list_' + i).boundingClientRect(function (res) {
        if (res.top >= 90 && res.top <= 200) {
          that.setData({
            activeIndex: i
          })
        }
      }).exec()
    }
  },
  onShareAppMessage: function () {
    return {
      title: '速冻在线',
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh:function(){
    this.onLoad()
    this.onShow()
  },
  onShow: function () {
    let that = this
    app.cart_num(this)
    app.new_order(this)
  },
  onLoad: function () {
    var that = this
    this.animation = wx.createAnimation({
      duration: 500,
    })
    wx.showLoading({
      title: '加载中',
    })
    app.ajax({
      url: app.host + "mall/adv?key=index_banner",
      success:function(res){
        that.setData({
          banner_list:res.data.data
        })
      }
    })
    app.ajax({
      url: app.host + "mall/adv?key=index_quick",
      success:function(res){
        that.setData({
          quick_list:res.data.data
        })
      }
    })
    app.ajax({
      url: app.host + "mall/adv?key=index_brand",
      success: function (res) {
        that.setData({
          brand_list: res.data.data
        })
      }
    })
    app.ajax({
      url: app.host + 'mall/goods/recgoods',
      success: function (res) {
        var used_title = res.data.data
        for (let i in used_title) {
          for (let j in used_title[i].list) {
            used_title[i].list[j].par_index = i
            used_title[i].list[j].index = j
            used_title[i].list[j].show_spece = true
            used_title[i].list[j].choose_text = "选规格"
          }
        }
        that.setData({
          used_title: used_title
        })
        wx.createSelectorQuery().select('.nav_box').boundingClientRect(function (rect) {
          that.nav_top = rect.top
          that.nav_height = rect.height
        }).exec()
        wx.createSelectorQuery().select('.index_header').boundingClientRect(function (rect) {
          that.header_height = rect.height
        }).exec()
        wx.createSelectorQuery().selectViewport().scrollOffset(function (res) {
          that.scrollTop = res.scrollTop  // 节点的竖直滚动位置
        }).exec()
        wx.hideLoading()
        wx.stopPullDownRefresh()
      }
    })  
  },
  advclick:function(e){
    var adv_id = e.currentTarget.dataset.adv_id
    var redirect = e.currentTarget.dataset.redirect
    if (redirect){
      wx.navigateTo({
        url: '/pages/banner_shop/banner_shop?adv_id=' + adv_id,
      })
    }
  },
  choose_G: function (e) {
    var index = e.currentTarget.dataset.index
    var par_index = e.currentTarget.dataset.par_index
    var used_title = this.data.used_title
    if (used_title[par_index].list[index].show_spece) {
      used_title[par_index].list[index].show_spece = false
      used_title[par_index].list[index].choose_text = "收起"
    } else {
      used_title[par_index].list[index].show_spece = true
      used_title[par_index].list[index].choose_text = "选规格"
    }
    this.setData({
      used_title: used_title
    })
  },
  add_cart: function (e) {
    app.ball_animate(e,this)
    var that = this
    var used_title = this.data.used_title
    var specval_id = e.currentTarget.dataset.specval_id
    for (let i in used_title) {
      for (let j in used_title[i].list) {
        if (used_title[i].list[j].specval_id == specval_id) {
          used_title[i].list[j].loading = true
        }
        for (let k in used_title[i].list[j].list) {
          if (used_title[i].list[j].list[k].specval_id == specval_id) {
            used_title[i].list[j].list[k].loading = true
            break
          }
        }
      }
    }
    this.setData({
      used_title: used_title
    })
    app.add_cart(e, this,function(id){
    var used_title = that.data.used_title
      for (let i in used_title) {
        for (let j in used_title[i].list) {
          if (used_title[i].list[j].specval_id == id) {
            used_title[i].list[j].loading = false
          }
          for (let k in used_title[i].list[j].list) {
            if (used_title[i].list[j].list[k].specval_id == id) {
              used_title[i].list[j].list[k].loading = false
              break
            }
          }
        }
      }
      that.setData({
        used_title: used_title
      })
    });
  },
  add_freq: function (e) {
    app.ajax({
      url: app.host + 'mall/Freqlist/add_freq',
      data: {
        goods_id: e.currentTarget.dataset.goods_id
      },
      success: function (res) {
        console.log(res);
          wx.showToast({
            title: res.data.msg,
          })
      },
    });
  }
});
